// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   discussion_check_ok.java

package draft.intrmanufacture;

import include.nseer_cookie.exchange;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;
import validata.ValidataTag;

public class discussion_check_ok extends HttpServlet
{

	ServletContext application;
	HttpSession session;
	nseer_db_backup1 erp_db;

	public discussion_check_ok()
	{
		erp_db = null;
	}

	public synchronized void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		ServletContext dbApplication = dbSession.getServletContext();
		try
		{
			nseer_db_backup1 intrmanufacture_db = new nseer_db_backup1(dbApplication);
			if (intrmanufacture_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				ValidataNumber validata = new ValidataNumber();
				ValidataTag vt = new ValidataTag();
				String discussion_ID = request.getParameter("discussion_ID");
				if (vt.validata((String)dbSession.getAttribute("unit_db_name"), "intrmanufacture_discussion", "discussion_ID", discussion_ID, "check_tag").equals("9") || vt.validata((String)dbSession.getAttribute("unit_db_name"), "intrmanufacture_discussion", "discussion_ID", discussion_ID, "check_tag").equals("5"))
				{
					String register = request.getParameter("register");
					String register_id = request.getParameter("register_id");
					String product_amount = request.getParameter("product_amount");
					int num = Integer.parseInt(product_amount);
					int n = 0;
					for (int i = 1; i <= num; i++)
					{
						String tem_amount = (new StringBuilder("amount")).append(i).toString();
						String tem_off_discount = (new StringBuilder("off_discount")).append(i).toString();
						String tem_list_price = (new StringBuilder("list_price")).append(i).toString();
						String amount = request.getParameter(tem_amount);
						String off_discount = request.getParameter(tem_off_discount);
						String list_price2 = request.getParameter(tem_list_price);
						StringTokenizer tokenTO2 = new StringTokenizer(list_price2, ",");
						String list_price;
						String list_price1;
						for (list_price = ""; tokenTO2.hasMoreTokens(); list_price = (new StringBuilder(String.valueOf(list_price))).append(list_price1).toString())
							list_price1 = tokenTO2.nextToken();

						if (!validata.validata(amount) || !validata.validata(off_discount) || !validata.validata(list_price))
							n++;
					}

					if (n == 0)
					{
						String provider_ID = request.getParameter("provider_ID");
						String provider_name = request.getParameter("provider_name");
						String demand_contact_person = request.getParameter("demand_contact_person");
						String demand_contact_person_tel = request.getParameter("demand_contact_person_tel");
						String demand_contact_person_fax = request.getParameter("demand_contact_person_fax");
						String demand_pay_time = request.getParameter("demand_pay_time");
						String bodyc = new String(request.getParameter("remark").getBytes("UTF-8"), "UTF-8");
						String remark = exchange.toHtml(bodyc);
						try
						{
							String sql = (new StringBuilder("update intrmanufacture_discussion set discussion_ID='")).append(discussion_ID).append("',provider_ID='").append(provider_ID).append("',provider_name='").append(provider_name).append("',demand_contact_person='").append(demand_contact_person).append("',demand_contact_person_tel='").append(demand_contact_person_tel).append("',demand_contact_person_fax='").append(demand_contact_person_fax).append("',demand_pay_time='").append(demand_pay_time).append("',register='").append(register).append("',register_id='").append(register_id).append("',remark='").append(remark).append("',check_tag='0',discussion_tag='0',modify_tag='1' where discussion_ID='").append(discussion_ID).append("'").toString();
							intrmanufacture_db.executeUpdate(sql);
							double pay_amount_sum = 0.0D;
							double sale_price_sum = 0.0D;
							double cost_price_sum = 0.0D;
							n = 1;
							for (int i = 1; i <= num; i++)
							{
								String tem_amount = (new StringBuilder("amount")).append(i).toString();
								String tem_off_discount = (new StringBuilder("off_discount")).append(i).toString();
								String tem_list_price = (new StringBuilder("list_price")).append(i).toString();
								String tem_cost_price = (new StringBuilder("cost_price")).append(i).toString();
								String tem_amount_unit = (new StringBuilder("amount_unit")).append(i).toString();
								String amount = request.getParameter(tem_amount);
								String off_discount = request.getParameter(tem_off_discount);
								String list_price2 = request.getParameter(tem_list_price);
								StringTokenizer tokenTO2 = new StringTokenizer(list_price2, ",");
								String list_price;
								String list_price1;
								for (list_price = ""; tokenTO2.hasMoreTokens(); list_price = (new StringBuilder(String.valueOf(list_price))).append(list_price1).toString())
									list_price1 = tokenTO2.nextToken();

								String cost_price2 = request.getParameter(tem_cost_price);
								StringTokenizer tokenTO3 = new StringTokenizer(cost_price2, ",");
								String cost_price;
								String cost_price1;
								for (cost_price = ""; tokenTO3.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
									cost_price1 = tokenTO3.nextToken();

								String amount_unit = request.getParameter(tem_amount_unit);
								double subtotal = Double.parseDouble(list_price) * (1.0D - Double.parseDouble(off_discount) / 100D) * Double.parseDouble(amount);
								double cost_price_after_discount_sum = Double.parseDouble(cost_price) * Double.parseDouble(amount);
								sale_price_sum += subtotal;
								cost_price_sum += cost_price_after_discount_sum;
								pay_amount_sum += Double.parseDouble(amount);
								String sql1 = (new StringBuilder("update intrmanufacture_discussion_details set list_price='")).append(list_price).append("',amount='").append(amount).append("',amount_unit='").append(amount_unit).append("',cost_price='").append(cost_price).append("',off_discount='").append(off_discount).append("',subtotal='").append(subtotal).append("' where discussion_ID='").append(discussion_ID).append("' and details_number='").append(i).append("'").toString();
								intrmanufacture_db.executeUpdate(sql1);
							}

							String sql2 = (new StringBuilder("update intrmanufacture_discussion set sale_price_sum='")).append(sale_price_sum).append("',cost_price_sum='").append(cost_price_sum).append("' where discussion_ID='").append(discussion_ID).append("'").toString();
							intrmanufacture_db.executeUpdate(sql2);
							sql = (new StringBuilder("delete from intrmanufacture_workflow where object_id='")).append(discussion_ID).append("'").toString();
							intrmanufacture_db.executeUpdate(sql);
							List rsList = new ArrayList();
							String elem[] = new String[3];
							sql = "select id,describe1,describe2 from intrmanufacture_config_workflow where type_id='02'";
							for (ResultSet rset = intrmanufacture_db.executeQuery(sql); rset.next(); rsList.add(elem))
							{
								elem = new String[3];
								elem[0] = rset.getString("id");
								elem[1] = rset.getString("describe1");
								elem[2] = rset.getString("describe2");
							}

							if (rsList.size() == 0)
							{
								sql = (new StringBuilder("update intrmanufacture_discussion set check_tag='1',modify_tag='0' where discussion_ID='")).append(discussion_ID).append("'").toString();
								intrmanufacture_db.executeUpdate(sql);
							} else
							{
								for (Iterator ite = rsList.iterator(); ite.hasNext(); intrmanufacture_db.executeUpdate(sql))
								{
									elem = (String[])ite.next();
									sql = (new StringBuilder("insert into intrmanufacture_workflow(config_id,object_ID,describe1,describe2) values ('")).append(elem[0]).append("','").append(discussion_ID).append("','").append(elem[1]).append("','").append(elem[2]).append("')").toString();
								}

							}
						}
						catch (Exception ex)
						{
							ex.printStackTrace();
						}
						response.sendRedirect((new StringBuilder("draft/intrmanufacture/discussion_choose_attachment.jsp?discussion_ID=")).append(discussion_ID).toString());
					} else
					{
						response.sendRedirect((new StringBuilder("draft/intrmanufacture/discussion_ok_a.jsp?discussion_ID=")).append(discussion_ID).toString());
					}
				} else
				{
					response.sendRedirect("draft/intrmanufacture/discussion_ok.jsp?finished_tag=2");
				}
				intrmanufacture_db.commit();
				intrmanufacture_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception exception) { }
	}
}
